package cn.sdormitory.evaluation.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.common.utils.DataRoleUtil;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.evaluation.dao.WorkMonthInterviewDao;
import cn.sdormitory.evaluation.entity.WorkMonthInterviewEntity;
import cn.sdormitory.evaluation.entity.WorkWeekEntity;
import cn.sdormitory.evaluation.service.WorkMonthInterviewService;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDept;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkMonthInterviewServiceImpl extends ServiceImpl<WorkMonthInterviewDao, WorkMonthInterviewEntity> implements WorkMonthInterviewService {
    @Resource
    DataRoleUtil dataRoleUtil;

    @Override
    public IPage<WorkMonthInterviewEntity> pageList(WorkMonthInterviewEntity vo) {
        LambdaQueryWrapper<WorkMonthInterviewEntity> wrapper = new LambdaQueryWrapper<>();
        //获取查询当前用户能查询到的范围
        List<String> userNameList = dataRoleUtil.getDeptSubordinateUserList();
        wrapper.in(userNameList.size()>0,WorkMonthInterviewEntity::getRegistrant,userNameList);

        wrapper.orderBy(true,false,WorkMonthInterviewEntity::getCreatedTime);
        Page<WorkMonthInterviewEntity> page = page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
        return page;
    }

    @Override
    public int updateInterview(WorkMonthInterviewEntity vo) {
        if(StringUtils.isEmpty(vo.getInterviewContent())){
            throw new ApiException("访谈内容不能为空");
        }
        vo.setInterviewTime(DateUtil.formatDateTime(new Date()));
        vo.setModifyBy(SysUserUtils.getSysUser().getUsername());
        vo.setModifyTime(new Date());
        int i = this.baseMapper.updateById(vo);
        return i;
    }
}
